<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/img/logo.png">
    <!-- Place this tag in your head or just before your close body tag. -->
    <script async defer src="https://buttons.github.io/buttons.js"></script>
    <title>
        
          反射机制的相关类【转载】 - 宋正兵的博客 | zbsong Blog
        
    </title>

    <link rel="canonical" href="zbsong.top/article/jdk学习之路—反射机制的相关类/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS --> 
    <link rel="stylesheet" href="/css/beantech.min.css">

    <link rel="stylesheet" href="/css/donate.css">
    
    <!-- Pygments Highlight CSS -->
    <link rel="stylesheet" href="/css/highlight.css">

    <link rel="stylesheet" href="/css/widget.css">

    <link rel="stylesheet" href="/css/rocket.css">

    <link rel="stylesheet" href="/css/signature.css">

    <link rel="stylesheet" href="/css/toc.css">

    <!-- Custom Fonts -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> -->
    <!-- Hux change font-awesome CDN to qiniu -->
    <link href="https://cdn.staticfile.org/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <!-- Hux Delete, sad but pending in China
    <link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/
    css'>
    -->


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- ga & ba script hoook -->
    <script></script>
</head>


<!-- hack iOS CSS :active style -->
<body ontouchstart="">
	<!-- Modified by Yu-Hsuan Yen -->
<!-- Post Header -->
<style type="text/css">
    header.intro-header{
        
            background-image: url('https://api.dujin.org/bing/1920.php')
            /*post*/
        
    }
    
</style>

<header class="intro-header" >
    <!-- Signature -->
    <div id="signature">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                
                    <div class="post-heading">
                        <div class="tags">
                            
                              <a class="tag" href="/tags/#jdk学习之路" title="jdk学习之路">jdk学习之路</a>
                            
                        </div>
                        <h1>反射机制的相关类【转载】</h1>
                        <!-- <h2 class="subheading">谈谈反射机制相关的类</h2> -->
                        <span class="meta">
                            宋正兵 on
                            2020-12-09
                        </span>
                    </div>
                


                </div>
            </div>
        </div>
    </div>
</header>

    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">songzblink</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">主页</a>
                    </li>
					<li>
                        <a href="/archive/">归档</a>
                    </li>
					<li>
                        <a href="/tags/">标签</a>
                    </li>
					<li>
                        <a href="/about/">关于</a>
                    </li>
					<!--
					修改about在前面的问题
                    
                        
                    
                        
                        <li>
                            <a href="/about/">关于</a>
                        </li>
                        
                    
                        
                        <li>
                            <a href="/archive/">归档</a>
                        </li>
                        
                    
                        
                        <li>
                            <a href="/tags/">标签</a>
                        </li>
                        
                    
                    -->
                </ul>
            </div>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
    var $body   = document.body;
	// querySelector 获取文档中 id="demo" 的元素
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>


    <!-- Main Content -->
    <!-- Modify by Yu-Hsuan Yen -->

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Post Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

				
                <p>转载地址：<a href="https://www.jianshu.com/p/9be58ee20dee" target="_blank" rel="noopener">Java高级特性——反射</a></p>
<p>除了转载的内容外，加入了部分实验代码。</p>
<h1 id="反射机制的相关类">反射机制的相关类</h1>
<p>与Java反射相关的类如下：</p>
<table>
<thead>
<tr>
<th>类名</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>Class类</td>
<td>代表类的实体，在运行的 Java 应用程序中表示类和接口</td>
</tr>
<tr>
<td>Field类</td>
<td>代表类的成员变量（成员变量也称为类的属性）</td>
</tr>
<tr>
<td>Method类</td>
<td>代表类的方法</td>
</tr>
<tr>
<td>Constructor类</td>
<td>代表类的构造方法</td>
</tr>
</tbody>
</table>
<h1 id="class类">Class类</h1>
<p>它位于 java.lang 包中，其构造函数是私有的，由 JVM（类加载器）创建 Class 对象，我们可以通过 getClass() 方法获取到 Class 对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 私有构造函数，使得只有jvm可以创建该类的对象，这个私有构造函数还可以防止通过默认构造函数创建类对象</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="title">Class</span><span class="params">(ClassLoader loader)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 初始化final变量ClassLoader</span></span><br><span class="line">    classLoader = loader;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>利用 Class 对象，我们可以拿到创建的类的属性、方法等。</p>
<h2 id="class-类的作用">Class 类的作用</h2>
<ol>
<li>获取类中属性的类型</li>
<li>获取类中属性的名称</li>
<li>获取类的方法</li>
<li>获取类的基类等等</li>
<li>利用 Class 类完成反射</li>
</ol>
<h2 id="class-类的主要方法">Class 类的主要方法</h2>
<p><strong>获得类相关的方法</strong></p>
<p><strong>带有Declared修饰的方法可以反射到私有的方法，没有Declared修饰的只能用来反射公有的方法</strong></p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>asSubclass(Class<u> clazz)</u></td>
<td>把传递的类的对象转换成代表其子类的对象</td>
</tr>
<tr>
<td>Cast</td>
<td>把对象转换成代表类或是接口的对象</td>
</tr>
<tr>
<td>getClassLoader()</td>
<td>获得类的加载器</td>
</tr>
<tr>
<td>getClasses()</td>
<td>返回一个数组，数组中包含该类中所有公共类和接口类的对象</td>
</tr>
<tr>
<td>getDeclaredClasses()</td>
<td>返回一个数组，数组中包含该类中所有类和接口类的对象</td>
</tr>
<tr>
<td>forName(String className)</td>
<td>根据类名返回类的对象</td>
</tr>
<tr>
<td>getName()</td>
<td>获得类的完整路径名字</td>
</tr>
<tr>
<td>newInstance()</td>
<td>创建类的实例</td>
</tr>
<tr>
<td>getPackage()</td>
<td>获得类的包</td>
</tr>
<tr>
<td>getSimpleName()</td>
<td>获得类的名字</td>
</tr>
<tr>
<td>getSuperclass()</td>
<td>获得当前类继承的父类的名字</td>
</tr>
<tr>
<td>getInterfaces()</td>
<td>获得当前类实现的类或是接口</td>
</tr>
</tbody>
</table>
<p><strong>获得类中属性相关的方法</strong></p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>getField(String name)</td>
<td>获得某个公有的属性对象</td>
</tr>
<tr>
<td>getFields()</td>
<td>获得所有公有的属性对象</td>
</tr>
<tr>
<td>getDeclaredField(String name)</td>
<td>获得某个属性对象</td>
</tr>
<tr>
<td>getDeclaredFields()</td>
<td>获得所有属性对象</td>
</tr>
</tbody>
</table>
<p><strong>获得类中注解相关的方法</strong></p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>getAnnotation(Class<a> annotationClass)</a></td>
<td>返回该类中与参数类型匹配的公有注解对象</td>
</tr>
<tr>
<td>getAnnotations()</td>
<td>返回该类所有的公有注解对象</td>
</tr>
<tr>
<td>getDeclaredAnnotation(Class<a> annotationClass)</a></td>
<td>返回该类中与参数类型匹配的所有注解对象</td>
</tr>
<tr>
<td>getDeclaredAnnotations()</td>
<td>返回该类所有的注解对象</td>
</tr>
</tbody>
</table>
<p><strong>获得类中构造器相关的方法</strong></p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>getConstructor(Class…&lt;?&gt; parameterTypes)</td>
<td>获得该类中与参数类型匹配的公有构造方法</td>
</tr>
<tr>
<td>getConstructors()</td>
<td>获得该类的所有公有构造方法</td>
</tr>
<tr>
<td>getDeclaredConstructor(Class…&lt;?&gt; parameterTypes)</td>
<td>获得该类中与参数类型匹配的构造方法</td>
</tr>
<tr>
<td>getDeclaredConstructors()</td>
<td>获得该类所有构造方法</td>
</tr>
</tbody>
</table>
<p><strong>获得类中方法相关的方法</strong></p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>getMethod(String name, Class…&lt;?&gt; parameterTypes)</td>
<td>获得该类某个公有的方法</td>
</tr>
<tr>
<td>getMethods()</td>
<td>获得该类所有公有的方法</td>
</tr>
<tr>
<td>getDeclaredMethod(String name, Class…&lt;?&gt; parameterTypes)</td>
<td>获得该类某个方法</td>
</tr>
<tr>
<td>getDeclaredMethods()</td>
<td>获得该类所有方法</td>
</tr>
</tbody>
</table>
<p><strong>类中其他重要的方法</strong></p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>isAnnotation()</td>
<td>如果是注解类型则返回 true</td>
</tr>
<tr>
<td>isAnnotationPresent(Class&lt;? extends Annotation&gt; annotationClass)</td>
<td>如果是指定类型注解类型则返回 true</td>
</tr>
<tr>
<td>isAnonymousClass()</td>
<td>如果是匿名类则返回 true</td>
</tr>
<tr>
<td>isArray()</td>
<td>如果是一个数组类则返回 true</td>
</tr>
<tr>
<td>isEnum()</td>
<td>如果是枚举类则返回 true</td>
</tr>
<tr>
<td>isInstance(Object obj)</td>
<td>如果 obj 是该类的实例则返回 true</td>
</tr>
<tr>
<td>isInterface()</td>
<td>如果是接口类则返回 true</td>
</tr>
<tr>
<td>isLocalClass()</td>
<td>如果是局部类则返回 true</td>
</tr>
<tr>
<td>isMemberClass()</td>
<td>如果是内部类则返回 true</td>
</tr>
</tbody>
</table>
<p><strong>forName 方法</strong></p>
<ul>
<li>加载类： <code>public static Class&lt;?&gt; forName(String className) throws ClassNotFoundException</code></li>
</ul>
<p>输入需要加载的类的全路径名，得到类的 Class 对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JavaAPIDemo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		Class&lt;?&gt; cls = Class.forName(<span class="string">"cn.my.vo.Person"</span>);</span><br><span class="line">		System.out.println(cls.getName());<span class="comment">// 获取类的完整名称</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// Person类放在cn.my.vo开发包中</span></span><br></pre></td></tr></table></figure>
<p><strong>newInstance 方法</strong></p>
<p>代替关键字 new，提供了对象的反射实例化方法。</p>
<p>在 JDK 1.9 之前的实例化：<code>public T newInstance() throws InstantiationException, IllegalAccessException</code></p>
<p>范例：通过newInstance()方法实例化Person对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* package cn.my.vo中Person.java */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">	<span class="comment">// 任何情况下如果要实例化对象则一定要调用类中的构造方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Person</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"*****Person类构造方法*****"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"一个Person类"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* JavaAPIDemo.java */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JavaAPIDemo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		Class&lt;?&gt; cls = Class.forName(<span class="string">"cn.my.vo.Person"</span>);</span><br><span class="line">		Object obj = cls.newInstance();<span class="comment">// 实例化对象，JDK 1.9后被废除了</span></span><br><span class="line">		System.out.println(obj); <span class="comment">// 输出对象调用toString()方法</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* output</span></span><br><span class="line"><span class="comment">【cls.newInstance()、关键字new】*****Person类构造方法*****</span></span><br><span class="line"><span class="comment">【System.out.println(obj)】一个Person类</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p>在 JDK 1.9 之后：<code>clazz.getDeclaredConstructor().newInstance()</code></p>
<p>范例：替代newInstance()方法实例化Person对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JavaAPIDemo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">		Class&lt;?&gt; cls = Class.forName(<span class="string">"cn.my.vo.Person"</span>);</span><br><span class="line">		Object obj = cls.getDeclaredConstructor().newInstance();</span><br><span class="line">		System.out.println(obj); <span class="comment">// 输出对象调用toString()方法</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>isInstance 方法</strong></p>
<p>isInstance() 方法与操作符 instanceof 功能是等价的。用于检查指定的对象是否兼容分配给该 Class 的实例。如果指定对象为非 null，并且可以强制转换为此类的实例，则该方法返回 true，否则返回 false。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestInfo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">"我是谁"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TestInfo</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"我是构造函数"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> String test=<span class="string">"测试属性"</span>;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        TestClass info=<span class="keyword">new</span> TestClass();</span><br><span class="line">        <span class="comment">//返回结果是true因为info是子类的对象</span></span><br><span class="line">		System.out.println(TestInfo.class.isInstance(info));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestClass</span> <span class="keyword">extends</span> <span class="title">TestInfo</span></span>&#123;&#125;</span><br></pre></td></tr></table></figure>
<p><strong>getName、getTypeName、getCanonicalName、getSimpleName  方法</strong> <a href="https://blog.csdn.net/Goodbye_Youth/article/details/83536840" target="_blank" rel="noopener">参考</a></p>
<p><code>public String getName()</code> 返回表示的类或接口的名称，例如<code>String.class.getName()</code> 将会返回 <code>&quot;java.lang.String&quot;</code> 。可以理解为返回的是虚拟机中 Class 对象的表示。同时会根据不同的类型，链接不同的符号加以表示。</p>
<p><code>public String getTypeName()</code> 返回此类型名称的信息字符串。可以理解为返回该类类型的名称。</p>
<p><code>public String getCanonicalName()</code>  返回基础类的规范名称（如果存在），否则为null。可以理解为返回的是正常的包含路径的类名。</p>
<p><code>public String getSimpleName()</code> 返回源代码中给出的基础类的简单名称，如果基础类是匿名的，则返回空字符串。可以理解为返回的仅仅是类名，不包含路径。</p>
<p>提供下面的例子，以便直观地感受四个方法之间的差异。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AAA</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">BBB</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数组</span></span><br><span class="line">        System.out.println(String[].class.getName()); <span class="comment">// [Ljava.lang.String;</span></span><br><span class="line">        System.out.println(String[].class.getCanonicalName()); <span class="comment">// java.lang.String[]</span></span><br><span class="line">        System.out.println(String[].class.getSimpleName()); <span class="comment">// String[]</span></span><br><span class="line">        System.out.println(String[].class.getTypeName()); <span class="comment">// java.lang.String[]</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 成员内部类</span></span><br><span class="line">        System.out.println(BBB.class.getName()); <span class="comment">// lang.reflect.AAA$BBB</span></span><br><span class="line">        System.out.println(BBB.class.getCanonicalName()); <span class="comment">// lang.reflect.AAA.BBB</span></span><br><span class="line">        System.out.println(BBB.class.getSimpleName()); <span class="comment">// BBB</span></span><br><span class="line">        System.out.println(BBB.class.getTypeName()); <span class="comment">// lang.reflect.AAA$BBB</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 匿名内部类</span></span><br><span class="line">        System.out.println(<span class="keyword">new</span> Object()&#123;&#125;.getClass().getName()); <span class="comment">// lang.reflect.AAA$1</span></span><br><span class="line">        System.out.println(<span class="keyword">new</span> Object()&#123;&#125;.getClass().getCanonicalName()); <span class="comment">// null</span></span><br><span class="line">        System.out.println(<span class="keyword">new</span> Object()&#123;&#125;.getClass().getSimpleName()); <span class="comment">// ""</span></span><br><span class="line">        System.out.println(<span class="keyword">new</span> Object()&#123;&#125;.getClass().getTypeName()); <span class="comment">// lang.reflect.AAA$4</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 普通类</span></span><br><span class="line">        System.out.println(AAA.class.getName()); <span class="comment">// lang.reflect.AAA</span></span><br><span class="line">        System.out.println(AAA.class.getCanonicalName()); <span class="comment">// lang.reflect.AAA</span></span><br><span class="line">        System.out.println(AAA.class.getSimpleName()); <span class="comment">// AAA</span></span><br><span class="line">        System.out.println(AAA.class.getTypeName()); <span class="comment">// lang.reflect.AAA</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 基本数据类型</span></span><br><span class="line">        System.out.println(<span class="keyword">int</span>.class.getName()); <span class="comment">// int</span></span><br><span class="line">        System.out.println(<span class="keyword">int</span>.class.getCanonicalName()); <span class="comment">// int</span></span><br><span class="line">        System.out.println(<span class="keyword">int</span>.class.getSimpleName()); <span class="comment">// int</span></span><br><span class="line">        System.out.println(<span class="keyword">int</span>.class.getTypeName()); <span class="comment">// int</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="field类">Field类</h1>
<p>Field代表类的成员变量（成员变量也称为类的属性）。</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>equals(Object obj)</td>
<td>属性与 obj 相等则返回 true</td>
</tr>
<tr>
<td>get(Object obj)</td>
<td>获得 obj 中对应的属性值</td>
</tr>
<tr>
<td>set(Object obj, Object value)</td>
<td>设置 obj 中对应属性值</td>
</tr>
</tbody>
</table>
<h1 id="method类">Method类</h1>
<p>Method代表类的方法。</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>invoke(Object obj, Object… args)</td>
<td>传递 object 对象及参数调用该对象对应的方法</td>
</tr>
</tbody>
</table>
<h1 id="constructor类">Constructor类</h1>
<p>Constructor代表类的构造方法。</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>用途</th>
</tr>
</thead>
<tbody>
<tr>
<td>newInstance(Object… initargs)</td>
<td>根据传递的参数创建类的对象</td>
</tr>
</tbody>
</table>

                
                <hr>
                <!-- Pager -->
                <ul class="pager">
                    
                        <li class="previous">
                            <a href="/article/jdk学习之路—反射/" data-toggle="tooltip" data-placement="top" title="浅谈反射">&larr; 上一篇</a>
                        </li>
                    
                    
                        <li class="next">
                            <a href="/article/jdk学习之路—Exception/" data-toggle="tooltip" data-placement="top" title="Exception异常类及其处理">下一篇 &rarr;</a>
                        </li>
                    
                </ul>

                <br>

                <!--打赏-->
                
                <!--打赏-->

                <br>
                <!--分享-->
                
                <!--分享-->
                <br>                       
                
                <!-- require APlayer -->
                
            </div>
            
            <!-- Tabe of Content -->
            <!-- Table of Contents -->

  
    <style>
      span.toc-nav-number{
        display: none
      }
    </style>
  
    
      <aside id="sidebar">
        <div id="toc" class="toc-article">
        <strong class="toc-title">目录</strong>
        
          <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#反射机制的相关类"><span class="toc-nav-number">1.</span> <span class="toc-nav-text">&#x53CD;&#x5C04;&#x673A;&#x5236;&#x7684;&#x76F8;&#x5173;&#x7C7B;</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#class类"><span class="toc-nav-number">2.</span> <span class="toc-nav-text">Class&#x7C7B;</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#class-类的作用"><span class="toc-nav-number">2.1.</span> <span class="toc-nav-text">Class &#x7C7B;&#x7684;&#x4F5C;&#x7528;</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#class-类的主要方法"><span class="toc-nav-number">2.2.</span> <span class="toc-nav-text">Class &#x7C7B;&#x7684;&#x4E3B;&#x8981;&#x65B9;&#x6CD5;</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#field类"><span class="toc-nav-number">3.</span> <span class="toc-nav-text">Field&#x7C7B;</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#method类"><span class="toc-nav-number">4.</span> <span class="toc-nav-text">Method&#x7C7B;</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#constructor类"><span class="toc-nav-number">5.</span> <span class="toc-nav-text">Constructor&#x7C7B;</span></a></li></ol>
        
        </div>
      </aside>
    

                
            <!-- Sidebar Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5><a href="/tags/">标签</a></h5>
                    <div class="tags">
                       
                          <a class="tag" href="/tags/#jdk学习之路" title="jdk学习之路">jdk学习之路</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
				<!--
                
                <hr>
                <h5>朋友们</h5>
                <ul class="list-inline">

                    
                        <li><a href="https://tyzhang.top/" target="_blank">ztygalaxy</a></li>
                    
                        <li><a href="http://www.yctang.club/" target="_blank">yctang</a></li>
                    
                        <li><a href="https://dolnw.github.io" target="_blank">JCWang</a></li>
                    
                </ul>
                
				-->
            </div>
        </div>
    </div>
</article>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>
<!-- anchor-js, Doc:http://bryanbraun.github.io/anchorjs/ -->
<script>
    async("https://cdn.bootcss.com/anchor-js/1.1.1/anchor.min.js",function(){
        anchors.options = {
          visible: 'hover',
          placement: 'left',
          icon: ''
        };
        anchors.add().remove('.intro-header h1').remove('.subheading').remove('.sidebar-container h5');
    })
</script>
<style>
    /* place left on bigger screen */
    @media all and (min-width: 800px) {
        .anchorjs-link{
            position: absolute;
            left: -0.75em;
            font-size: 1.1em;
            margin-top : -0.1em;
        }
    }
</style>


<!-- chrome Firefox 中文锚点定位失效-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<!-- smooth scroll behavior polyfill  -->
<script type="text/javascript" src="/js/smoothscroll.js"></script>
<script>
        $('#toc').on('click','a',function(a){
            // var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
            // console.log(window.navigator.userAgent,isChrome)
                // if(isChrome) {
                    // console.log(a.currentTarget.outerHTML);
                    // console.log($(a.currentTarget).attr("href"));
                    //跳转到指定锚点
                    // document.getElementById(a.target.innerText.toLowerCase()).scrollIntoView(true);
                    document.getElementById($(a.currentTarget).attr("href").replace("#","")).scrollIntoView({behavior: 'smooth' });
                // }
        })  
</script>


    <!-- Footer -->
    <!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                
                
                
				
                

                

                
                    <li>
                        <a target="_blank"  href="https://github.com/songzblink">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; 宋正兵 2021 
                    <br>
                    Theme by <a href="http://www.huweihuang.com">Huwei Huang</a>
                    re-Ported by <a href="https://github.com/ztygalaxy">ztygalaxy</a>
					
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/hux-blog.min.js"></script>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- 
     Because of the native support for backtick-style fenced code blocks 
     right within the Markdown is landed in Github Pages, 
     From V1.6, There is no need for Highlight.js, 
     so Huxblog drops it officially.

     - https://github.com/blog/2100-github-pages-now-faster-and-simpler-with-jekyll-3-0  
     - https://help.github.com/articles/creating-and-highlighting-code-blocks/    
-->
<!--
    <script>
        async("http://cdn.bootcss.com/highlight.js/8.6/highlight.min.js", function(){
            hljs.initHighlightingOnLoad();
        })
    </script>
    <link href="http://cdn.bootcss.com/highlight.js/8.6/styles/github.min.css" rel="stylesheet">
-->


<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("zbsong.top/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>


<!-- Google Analytics -->




<!-- Baidu Tongji -->






	<a id="rocket" href="#top" class=""></a>
	<script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
    <script type="text/javascript" src="/js/toc.js?v=1.0.0" async=""></script>
<!-- Image to hack wechat -->
<img src="zbsong.top/img/icon_wechat.png" width="0" height="0" />
<!-- Migrate from head to bottom, no longer block render and still work -->

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<!--
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</body>

</html>
